﻿
using System.Collections.Generic;
using System.Data;
using System;
namespace ZHDJ.MobileInterface
{
    public partial class MyTaskLeave : AbstractBasePage
    {
        protected override OutParam BusinessProcess<InParam, OutParam>(InParam inParam)
        {
            // 业务逻辑返回的响应对象  
            MyTaskLeaveOutParam result = new MyTaskLeaveOutParam();
            MyTaskLeaveInParam param = new MyTaskLeaveInParam();
            string logMsg = "";
            bool isFlag = true;
            string category = "";
                 
            try
            {
                param = inParam as MyTaskLeaveInParam;
                if (string.IsNullOrEmpty(param.user_id) || string.IsNullOrEmpty(param.todo_id) || string.IsNullOrEmpty(param.todo_rel_id))
                {
                    result.code = "0";
                    result.desc = "用户ID,关系ＩＤ,以及待办ID不能为空";

                    isFlag = false;
                }


                if (CommonUtils.IsDisableUsers(param.user_id))
                {
                    result.code = "0";
                    result.desc = "用户状态有问题，请联系管理员";

                    isFlag = false;
                }
                if(isFlag)
                {
                    //获取待办信息
                    DataSet dsTask = InterfaceHelper.GetPartyTaskByID(param.todo_id);

                    if (dsTask == null || dsTask.Tables[0].Rows.Count == 0)
                    {
                        result.code = "0";
                        result.desc = "待办不存在";

                        isFlag = false;
                    }
                    else if (dsTask.Tables[0].Rows[0]["RelationID"].ToString() != param.todo_rel_id.Trim())
                    {
                        result.code = "0";
                        result.desc = "该关系ID不属于该待办";

                        isFlag = false;
                    }
                    else
                    { 
                        category = dsTask.Tables[0].Rows[0]["category"].ToString();
                    }
                }

                if (isFlag)
                {
                    if (category != "普通党课" && category != "视频党课" && category != "掌上党课" &&
                        category != "普通会议" && category != "视频会议" && category != "掌上会议" &&
                        category != "普通活动" && category != "视频活动" && category != "掌上活动" && category != "支部活动" &&
                        category != "志愿活动" && category != "志愿活动-活动管理")
                    {
                        result.code = "0";
                        result.desc = "类型为［" + category + "］的待办不能请假";

                        isFlag = false;
                    }
                }

                if (isFlag)
                {
                    InterfaceHelper.MaTaskJoinOrLeave(param.todo_rel_id.Trim(), param.user_id.Trim(), category, "请假确认");

                    InterfaceHelper.TaskDoneByTaskID(param.todo_id, param.user_id);
                }

                logMsg = result.desc;
            }
            catch (Exception ex)
            {
                result.code = "0";
                result.desc = "操作异常，多次出现请联系管理员"; 
                logMsg = ex.Message + ":" + ex.StackTrace;
            }

            CommonUtils.InsertMobileInterfaceLog(SystemCode +"-MyTaskLeave", SerializeHelper.JsonSerializer<MyTaskLeaveInParam>(param), logMsg); 
            return result as OutParam;
        }

        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (string.Compare(Request.QueryString["Safe"], "False", true) == 0)
                ProcessRequest<MyTaskLeaveInParam, MyTaskLeaveOutParam>();
            else
                SafeProcessRequest<MyTaskLeaveInParam, MyTaskLeaveOutParam>();
        }
    }

    #region 请求参数
    public class MyTaskLeaveInParam
    {
        /// <summary>
        /// 必填 用户ID
        /// </summary>
        public string user_id
        { get; set; }

        /// <summary>
        /// 必填 待办ID
        /// </summary>
        public string todo_id
        { get; set; }

        /// <summary>
        /// 必填 关系ＩＤ
        /// </summary>
        public string todo_rel_id
        { get; set; } 
    }
    #endregion

    #region 结果参数
    public class MyTaskLeaveOutParam
    {
        public MyTaskLeaveOutParam()
        {
            code = "1";
            desc = "操作成功"; 
        }

        /// <summary>
        /// 返回结果 具体见接口返回对照码 1	成功 0	失败 2	请求非法 加密结果和传入的MD5字符串进行比较，如果相同则执行接口操作，否则返回1001错误响应码
        /// </summary>
        public string code
        { get; set; }

        /// <summary>
        /// 结果描述 成功和失败都需要
        /// </summary>
        public string desc
        { get; set; } 
    }  
    #endregion 
}